# In Government We Trust: Implicit Political Trust and Regime Support in China 
# weighting replication file

# install package if not installed already
install.packages("survey")

# load libraries
library(survey)

# load data, fyi: change file location
data <-read.csv("--file path--/IAT_China_replication_data.csv")

# calculate weights using raking with 2018 CFPS demographics with subsetted data

# subset data without missing ccpmember data
sdata <- subset(data, data$ccpmember!= ".")

# collapse age categories from 10 to 6
sdata$age_c[sdata$agegroup == 1] <- 1
sdata$age_c[sdata$agegroup == 2] <- 2
sdata$age_c[sdata$agegroup == 3] <- 2
sdata$age_c[sdata$agegroup == 4] <- 3
sdata$age_c[sdata$agegroup == 5] <- 3
sdata$age_c[sdata$agegroup == 6] <- 4
sdata$age_c[sdata$agegroup == 7] <- 4
sdata$age_c[sdata$agegroup == 8] <- 5
sdata$age_c[sdata$agegroup == 9] <- 5
sdata$age_c[sdata$agegroup == 10] <- 6

# collapse education categories from 6 to 5
sdata$edu_c[sdata$education == 1] <-1
sdata$edu_c[sdata$education == 2] <-2
sdata$edu_c[sdata$education == 3] <-3
sdata$edu_c[sdata$education == 4] <-4
sdata$edu_c[sdata$education == 5] <-5
sdata$edu_c[sdata$education == 6] <-5

# remove "."
sdata$ccp_c[sdata$ccpmember == 0] <- 0
sdata$ccp_c[sdata$ccpmember == 1] <- 1

##-- weight to internet active population--

# create unweighted data object
data.unweighted <- svydesign(ids = ~1, data = sdata)

# create population data frames for internet active adults
# female: male= 0, female= 1
# ccp member: no= 0, yes= 1
# age group: 19 or below= 1, 60 or above= 6
# education: primary or below= 1, 4 year college or above= 5
# urban: rural= 0, urban= 1

female.dist <- data.frame(female = c("0", "1"),
                          Freq = nrow(data) * c(0.513, 0.487))

age_c.dist <- data.frame(age_c = c("1", "2", "3", "4", "5", "6"),
                         Freq = nrow(data) * c(0.045, 0.295, 0.277, 0.19, 0.122, 0.07))

edu_c.dist <- data.frame(edu_c= c("1", "2", "3", "4", "5"),
                         Freq = nrow(data)* c(0.135, 0.346, 0.228, 0.146, 0.145))

urban.dist <- data.frame(urban= c("0", "1"),
                         Freq = nrow(data) * c(0.389, 0.611))

ccp_c.dist <- data.frame(ccp_c = c("0", "1"),
                         Freq = nrow(data)* c(0.883, 0.117))

# rake weight data
data.rake <- rake(design = data.unweighted,
                  sample.margins = list(~female, ~age_c, ~edu_c, ~urban, ~ccp_c),
                  population.margins = list(female.dist, age_c.dist, edu_c.dist, urban.dist, ccp_c.dist))

# trim weights
data.rake.trim <- trimWeights(data.rake, lower = 0.3, upper = 3, strict = TRUE)

# examine trimmed weights
data.rake.trim$prob

# add weights column to original data
sdata$weights2018_internet <- data.rake.trim$prob

# weighted mean for iat
weighted.mean(sdata$diatgov, sdata$weights2018_internet)

# weighted mean for trust index
weighted.mean(sdata$trust_index, sdata$weights2018_internet)

# percentage with high implicit trust
sum(sdata$weights2018_internet[sdata$diatgov > 0])/(sum(sdata$weights2018_internet))

# percentage with high explicit trust
sum(sdata$weights2018_internet[sdata$trust_index > 50])/(sum(sdata$weights2018_internet))

# --weight to adult population--

# create population data frames for adults
# female: male= 0, female= 1
# ccp member: no: 0, yes: 1
# age group: 19 or below= 1, 60 or above= 6
# education: primary or below= 1, 4 year college or above= 5
# urban: rural= 0, urban= 1

female.dist <- data.frame(female = c("0", "1"),
                          Freq = nrow(data) * c(0.494, 0.506))

age_c.dist <- data.frame(age_c = c("1", "2", "3", "4", "5", "6"),
                         Freq = nrow(data) * c(0.027, 0.157, 0.172, 0.185, 0.198, 0.262))

edu_c.dist <- data.frame(edu_c= c("1", "2", "3", "4", "5"),
                         Freq = nrow(data)* c(0.373, 0.325, 0.159, 0.076, 0.067))

urban.dist <- data.frame(urban= c("0", "1"),
                         Freq = nrow(data) * c(0.477, 0.523))

ccp_c.dist <- data.frame(ccp_c = c("0", "1"),
                         Freq = nrow(data)* c(0.903, 0.097))

# rake weight data
data.rake <- rake(design = data.unweighted,
                  sample.margins = list(~female, ~age_c, ~edu_c, ~urban, ~ccp_c),
                  population.margins = list(female.dist, age_c.dist, edu_c.dist, urban.dist, ccp_c.dist))

# trim weights
data.rake.trim <- trimWeights(data.rake, lower = 0.3, upper = 3, strict = TRUE)

# examine trimmed weights
data.rake.trim$prob

# add weights column to original data
sdata$weights2018_adults <- data.rake.trim$prob

# weighted mean for iat
weighted.mean(sdata$diatgov, sdata$weights2018_adult)

# weighted mean for trust index
weighted.mean(sdata$trust_index, sdata$weights2018_adult)

# percentage with high implicit trust
sum(sdata$weights2018_adult[sdata$diatgov > 0])/(sum(sdata$weights2018_adult))

# percentage with high explicit trust
sum(sdata$weights2018_adult[sdata$trust_index > 50])/(sum(sdata$weights2018_adult))
